package com.chuyangc.qdms.controller;

import com.chuyangc.qdms.entity.Logrec;
import com.chuyangc.qdms.service.LogrecService;
import com.chuyangc.qdms.vo.LogrecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欧楚杨
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/chuyangc/logrec")
@Api(tags = "日志相关的请求")
public class LogrecController {
    @Autowired
    LogrecService logrecService;

    @GetMapping("/pageinslist/{pageNum}")
    @ApiOperation("获取登入状态列表每页显示十条记录")
    public PageInfo<Logrec> pageListLogrecins(@PathVariable String pageNum){
        int pageSize = 10;
        PageHelper.startPage(Integer.parseInt(pageNum),pageSize);
        List<Logrec> logrecins = logrecService.getLogrecins();
        PageInfo<Logrec> logrecPageInfo = new PageInfo<>(logrecins);
        return logrecPageInfo;
    }

    @GetMapping("/pageoutslist/{pageNum}")
    @ApiOperation("获取登出状态列表每页显示十条记录")
    public PageInfo<Logrec> pageListLogrecouts(@PathVariable String pageNum){
        int pageSize = 10;
        PageHelper.startPage(Integer.parseInt(pageNum),pageSize);
        List<Logrec> logrecouts = logrecService.getLogrecouts();
        PageInfo<Logrec> logrecPageInfo = new PageInfo<>(logrecouts);
        return logrecPageInfo;
    }

    @PostMapping("/add")
    @ApiOperation("插入日志记录")
    public boolean addLog(@RequestBody Logrec logrec){
        return logrecService.save(logrec);
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation("删除日志记录")
    public boolean delLog(@PathVariable Integer id){
        return logrecService.removeById(id);
    }

    @PostMapping("/setinvert")
    @ApiOperation("对logtype的值进行取反操作")
    public boolean setInvert(@RequestBody Logrec logrec){
        if (logrec.getLogtype() == 1){
            logrec.setLogtype(0);
        }else {
            logrec.setLogtype(1);
        }
        return logrecService.updateById(logrec);
    }

    @GetMapping("/logchart")
    @ApiOperation("不同登录状态下的日志记录数统计")
    public LogrecVO getCount(){
        return logrecService.getCount();
    }

    @GetMapping("/total")
    @ApiOperation("获取所有日志记录的数量")
    public Integer getTotal(){
        return logrecService.count();
    }

    @GetMapping("/list")
    @ApiOperation("获取所有日志记录")
    public List<Logrec> getLoglist(){
        return logrecService.list();
    }
}
